var P2P = {
	M$ : function(id){return document.getElementById(id)},
	M$$ : function(c,p){return p.getElementsByTagName(c)},
	newlist : function(id){
		var oBox=document.getElementById(id);
		var oUl=oBox.getElementsByTagName('ul')[0];
		var aLi=oUl.getElementsByTagName('li');
		var aDiv=oBox.getElementsByTagName('div');
		for(var i=0;i<aLi.length;i++){
			aLi[i].index=i;
			aLi[i].onmouseover=function(){
				for(var i=0;i<aLi.length;i++){
					aDiv[i].style.display='none';
					aLi[i].className='';
				}
				aDiv[this.index].style.display='block';
				this.className='active';
			}
		}
	},
	
	indexSearch : function(){
		var ind = 20;
		fntab();
		function bind(obj,ev,fn){
			if( obj.addEventListener ){
				obj.addEventListener(ev,fn,false);
			}else{
				obj.attachEvent('on' + ev,function(){
					fn.call(obj);	
				});
			}
		}
		bind(document,'click',function(){
			var aDet = getByClass('det',document);
			for(var i=0;i<aDet.length;i++){
				aDet[i].style.display = 'none';
			}
		});
		bind(document,'click',function(){
			var aDett = getByClass('dett',document);
			for(var i=0;i<aDett.length;i++){
				aDett[i].style.display = 'none';
			}
		});

		$("#ul1 li").click(function(event){
			$(this).addClass("s");
			$(this).siblings().removeClass("s");
		});

		function fntab(){
			var oUl=document.getElementById('ul1');
			var aLi=oUl.getElementsByTagName('li');	
			var banner = document.getElementById('banner');
			var aDiv = getByClass('detail-1',document);
			var bansec = getByClass('banner-search',document)[0];
			var aInp = getByClass('input',document);
			var aInp1 = getByClass('input1',document);
			function cd(a,b){
				var aaa = getByClass(a,document);
				var aDet = getByClass(b,document);
				var arr = [];
				for(var i=0;i<aaa.length;i++){
					arr.push(aaa[i].getElementsByTagName('input')[0]);
				};
				for(var i=0;i<aInp.length;i++){
					aDet[i].style.display = 'none';
					aInp[i].index = i;
					aInp[i].onclick = function(ev){
						var ev = ev || event;
						var that = this.parentNode.parentNode;
						that.style.zIndex = ind++;
						if(aDet[this.index].style.display == 'block'){
							for(var i=0;i<aDet.length;i++){
								aDet[i].style.display = 'none';	
							}	
							aDet[this.index].style.display = 'none';
						}else if(aDet[this.index].style.display == 'none'){
							for(var i=0;i<aDet.length;i++){
								aDet[i].style.display = 'none';
							}
							aDet[this.index].style.display = 'block';
						}
						ev.cancelBubble = true;
					}
				};
				var ar = [];
				for(var i=0;i<aDet.length;i++){
					for(var j=0;j<aDet[i].getElementsByTagName('p').length;j++){
						ar.push(aDet[i].getElementsByTagName('p')[j]);
					}	
				}	
				for(var i=0;i<ar.length;i++){
					ar[i].index = i;
					ar[i].onclick = function(){
						this.parentNode.parentNode.getElementsByTagName('input')[0].value = this.getAttribute('_value');
						this.parentNode.parentNode.getElementsByTagName('div')[0].innerHTML=this.innerHTML;
					}	
					ar[i].onmouseover = function(){
						for(var i=0;i<ar.length;i++){
							ar[i].style.background = '';	
						}
						ar[this.index].style.background = '#ebebeb'
					}
				}
			};
			function tabul(a){
				//for(var i=0;i<aLi.length;i++){
				//	aLi[i].style.color = '#252f3b';
				//	aLi[i].style.borderBottom = '1px solid #ccc5c7';
				//}
				//a.style.color = '#da5912';
				//a.style.borderBottom = 'none';	
			};
			cd('aaa','det');
			aLi[0].onclick = function(){
				for(var i=1;i<aDiv.length;i++){
					aDiv[i].style.display = 'none';	
				}
				aDiv[0].style.display = 'block';
				bansec.style.display = 'block';
				//for(var i=0;i<aLi.length;i++){
				//	aLi[i].style.background='#f2edf1';
				//};
				tabul(this);
				cd('aaa','det');
			}
			aLi[1].onclick = function(){
				var aInp1 = getByClass('input1',document);
				var aDet = getByClass('dett',document);
				for(var i=0;i<aDiv.length;i++){
					aDiv[i].style.display = 'none';	
				};
				aDiv[1].style.display = 'block'	;
				bansec.style.display = 'block';
				//for(var i=0;i<aLi.length;i++){
				//	aLi[i].style.background='#f2edf1';
				//};
				tabul(this);
				cd('bbb','dett');
				for(var i=0;i<aInp1.length;i++){
					aInp1[i].index = i;
					aInp1[i].onclick = function(ev){
						var ev = ev || event;
						var that = this.parentNode.parentNode;
						that.style.zIndex = ind++;
						if(aDet[this.index].style.display == 'none'){
							for(var i=0;i<aDet.length;i++){
								aDet[i].style.display = 'none';	
							}	
							aDet[this.index].style.display = 'block';
						}else if(aDet[this.index].style.display == 'block'){
							for(var i=0;i<aDet.length;i++){
								aDet[i].style.display = 'none';	
							}
							aDet[this.index].style.display = 'none';
						}
						ev.cancelBubble = true;
					}
				}
			}
		}	
	},
	
	appSort : function(){
		$(".appSort .more").toggle(function(){
			$(this).parent().addClass("open");
		},function(){
			$(this).parent().removeClass("open");
		})
	},

	hotApp : function(){
		$(".hotApp li").mousemove(function(event){
			$(this).addClass("s");
			$(this).siblings().removeClass("s");
		});
		$(".hotApp li").hover(function(event){
			$(this).children(".codebox").show();
		},function(){
			$(this).children(".codebox").hide();
		});
	},

	list2Code : function(){
		var applist = document.getElementById("appList").getElementsByTagName("li");
		for (i = 0; i < applist.length; i++){
			applist[i].style.zIndex = 1000-i;
		}
		$(".applist li").hover(function(event){
			$(this).addClass("s");
		},function(){
			$(this).removeClass("s");
		});
	},

	appTop5list : function(){
		$(".appTop5list li").hover(function(event){
			$(this).addClass("s");
		},function(){
			$(this).removeClass("s");
		});
	},

	indexTop: function(){
		$uLi=$('.max').find('li')
		$oLi=$('.min').find('li')
		num=0;
		timer=null;
		$oLi.click(function(){
			var index=$(this).index();
			$(this).addClass('on').siblings().attr('class','');
			$uLi.css('display','none');
			$uLi.css('opacity',0);
			$uLi.eq($(this).index()).css('display','block');
			$uLi.eq($(this).index()).animate({opacity:1},500,'linear');
			num=index;
		});
		tab();
		$('#banner-tab').mouseover(function(){
			clearInterval(timer);
		})
		$('#banner-tab').mouseout(function(){
			tab();
		})
	},

	indexPage : function(){
		this.newlist('box1');
		this.hotApp();
		this.indexTop();
	},

	appListPage : function(){
		this.hotApp();
	}
}

var MO = function(){
	var M$ = function(id){return document.getElementById(id)}, M$$ = function(c,p){return p.getElementsByTagName(c)};
	var FocusPic = function(o) {
		this.setting      = typeof o === 'object' ? o : {};
		this.target       = this.setting.targetElement;
		this.showMarkers  = this.setting.showMarkers || false;
		this.showControls = this.setting.showControls || false;
		this.timer        = null;
		this.currentTime  = null;
		this.ms           = 40;
		this.autoMs       = 3000;
		this.iTarget      = 0;
		this.nextTarget   = 0;
		this.speed        = 0;
		this.init();
		this.handleEvent();
	};
	FocusPic.prototype = {
		init: function() {
			this.obj      = M$(this.target);
			this.oUl      = M$$("ul",this.obj)[0];
			this.aUlLis   = M$$("li",this.oUl);
			this.width    = this.aUlLis[0].offsetWidth;
			this.number   = this.aUlLis.length;
			this.oUl.style.width = this.width * this.number + 'px';
			if(this.showMarkers) {
				var oDiv = document.createElement('div');
				var aLis = [];
				for(var i = 0; i < this.number; i++) {
					aLis.push('<li>&nbsp;<\/li>');
				};
				oDiv.innerHTML = '<ol>'+ aLis.join('') +'<\/ol>';
				this.obj.appendChild(oDiv.firstChild);
				this.aLis = M$$("li",M$$("ol",this.obj)[0]);
				this.aLis[0].className = 'active';
				oDiv = null;
			};
			if(this.showControls) {
				this.oPrev = document.createElement('a');
				this.oNext = document.createElement('a');
				this.oPrev.setAttribute("href","javascript:void(0)");
				this.oNext.setAttribute("href","javascript:void(0)");
				this.oPrev.setAttribute("onfocus","this.blur()");
				this.oNext.setAttribute("onfocus","this.blur()");
				this.oPrev.className = 'prev';
				this.oPrev.innerHTML = '&laquo;';
				this.oNext.className = 'next';
				this.oNext.innerHTML = '&raquo;';
				this.obj.appendChild(this.oPrev);
				this.obj.appendChild(this.oNext);
			};
		},
		handleEvent: function() {
			var that = this;
			this.currentTime = setInterval(function() {
				that.autoPlay();
			}, this.autoMs);
			this.addEvent(this.obj, 'mouseover', function() {
				clearInterval(that.currentTime);
			});
			this.addEvent(this.obj, 'mouseout', function() {
				that.currentTime = setInterval(function() {
					that.autoPlay();
				}, that.autoMs);
			});
			if(this.showMarkers) {
				for(var i = 0; i < this.number; i++) {
					var el = this.aLis[i];
					(function(index) {
						that.addEvent(el, 'mouseover', function() {
							that.goTime(index);
						});
					})(i);
				};
			};
			if(this.showControls) {
				this.addEvent(this.oPrev, 'click', function() {
					that.fnPrev();
				});
				this.addEvent(this.oNext, 'click', function() {
					that.autoPlay();
				});
			};
		},
		addEvent: function(el, type, fn) {
			if(window.addEventListener) {
				el.addEventListener(type, fn, false);
			}
			else if(window.attachEvent) {
				el.attachEvent('on' + type, fn);
			};
		},
		fnPrev: function() {
			this.nextTarget--;
			if(this.nextTarget < 0) {
				this.nextTarget = this.number - 1;
			};
			this.goTime(this.nextTarget);
		},
		autoPlay: function() {
			this.nextTarget++;
			if(this.nextTarget >= this.number) {
				this.nextTarget = 0;
			};
			this.goTime(this.nextTarget);
		},
		goTime: function(index) {
			var that = this;
			if(this.showMarkers) {
				for(var i = 0; i < this.number; i++) {
					i == index ? this.aLis[i].className = 'active' : this.aLis[i].className = '';
				};
			};
			this.iTarget = -index * this.width;
			if(this.timer) {
				clearInterval(this.timer);
			};
			this.timer = setInterval(function() {
				that.doMove(that.iTarget);
			}, this.ms);
		},
		doMove: function(target) {
			this.oUl.style.left = this.speed + 'px';
			this.speed += (target - this.oUl.offsetLeft) / 3;
			if(Math.abs(target - this.oUl.offsetLeft) === 0) {
				this.oUl.style.left = target + 'px';
				clearInterval(this.timer);
				this.timer = null;
			};
		}
	};
	return {focuspic:function(o){new FocusPic(o)}};
}

function tab(){
	timer=setInterval(function(){
		num++;
		if(num==$uLi.length)num=0;
		$oLi.eq(num).attr('class','on').siblings().attr('class','');
		$uLi.css('display','none');
		$uLi.css('opacity',0);
		$uLi.eq(num).css('display','block');
		$uLi.eq(num).animate({opacity:1},500,'linear');
	},6000)
}

function getByClass(sClass, parent){
  var aEles = (parent || document).getElementsByTagName('*');
  var arr = [];
  for (var i = 0; i < aEles.length; i++) {
    var aClass = aEles[i].className.split(' ');
    for (var j = 0; j < aClass.length; j++) {
      if (aClass[j] == sClass) {
        arr.push(aEles[i]);
        break;
      }
    }
  }
  return arr;
}


$(function(){
	if (document.getElementById("indexPage")){
		P2P.indexPage();
		P2P.indexSearch();
		var focuspic = new MO();
		focuspic.focuspic({"targetElement":"focuspic","showMarkers":true,"showControls":true});
	}
	if (document.getElementById("appList")){
		if (document.getElementById("appTop5list")){
			P2P.appTop5list();
		}
		P2P.appListPage();
		P2P.appSort();
		P2P.list2Code();
	}
})


